﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiXiu.Model;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

namespace AiXiu.DAL
{
    public class LoginDAL
    {
        AiXiuDbContext db = new AiXiuDbContext();
        public TBLogins ExixtsName(string userName) {
            TBLogins tb = db.TBLogins.FirstOrDefault(a => a.UserName == userName);
            return tb;
        }
        //方法一 判断手机号
        public TBLogins ExixtsPhone1(string userPhone)
        {
            TBLogins tb = db.TBLogins.FirstOrDefault(a => a.MobileNumber == userPhone);
            return tb;
        }

      
            /// <summary>
            /// 用户名是否注册
            /// </summary>
            /// <param name="UserName"></param>
            /// <returns></returns>
        public bool ExistsUserName(string UserName) {
            return db.TBLogins.Any(a => a.UserName == UserName);
        }
        /// <summary>
        /// 手机号是否注册
        /// </summary>
        /// <param name="MobileNumber"></param>
        /// <returns></returns>
        public bool ExixtsPhone(string MobileNumber) {
            return db.TBLogins.Any(a => a.MobileNumber == MobileNumber);
        }
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="tb"></param>
        /// <returns></returns>
        public int ReZC(TBLogins tb) {
            SqlParameter[] sql = {
                new SqlParameter("@userName",tb.UserName),
                new SqlParameter("@mobileNumber",tb.MobileNumber),
                new SqlParameter("@password",tb.Password),
                new SqlParameter("@userId",SqlDbType.Int)
            };
            sql[3].Direction = ParameterDirection.Output;
            DbCommand cmd = db.Database.Connection.CreateCommand();
            cmd.CommandText = "proc_adduser";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(sql);
            // 执行存储过程
            if (cmd.Connection.State!=ConnectionState.Open)
            {
                cmd.Connection.Open();
            }
            cmd.ExecuteNonQuery();
            if (cmd.Connection.State==ConnectionState.Open)
            {
                cmd.Connection.Close();
            }
            // 返回结果
            return int.Parse(sql[3].Value.ToString());

        }
        public TBUsers  dataget(int id) {
            return db.TBUsers.Find(id);
        }
         

    }
}
